﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

using ZhimeiSoft.Framework.Server.Base;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;

namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class CustomerReturnVisitDAL: BaseDal, ICustomerReturnVisitDAL
    {
        private void AddInParameter(ref DbCommand cmd, DBEntityCustomerReturnVisit visit)
        {
            DB.AddInParameter(cmd, DBEntityCustomerReturnVisit.FieldName.CEMPLOYEEID, DbType.Int32, visit.CEMPLOYEEID);
            DB.AddInParameter(cmd, DBEntityCustomerReturnVisit.FieldName.CMEMO, DbType.String, visit.CMEMO);
            
            DB.AddInParameter(cmd, DBEntityCustomerReturnVisit.FieldName.CRETURNVISITTIME, DbType.DateTime, visit.CRETURNVISITTIME);
            DB.AddInParameter(cmd, DBEntityCustomerReturnVisit.FieldName.CUSTOMERID, DbType.Int32, visit.CUSTOMERID);
        }

        public void InsertCustomerReturnVisit(DBEntityCustomerReturnVisit visit)
        {
            string strSQL = CustomerReturnVisitDAL_Const.SQL_INSERT_CUSTOMERRETURNVISIT;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            AddInParameter(ref cmd, visit);
            DB.ExecuteNonQuery(cmd);
        }

        public void UpdateCustomerReturnVisit(DBEntityCustomerReturnVisit visit)
        {
            string strSQL = CustomerReturnVisitDAL_Const.SQL_UPDATE_CUSTOMERRETURNVISIT;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmd, DBEntityCustomerReturnVisit.FieldName.CRETURNVISITID, DbType.Int32, visit.CRETURNVISITID);
            AddInParameter(ref cmd, visit);
            DB.ExecuteNonQuery(cmd);
        }

        public void DeleteCustomerReturnVisit(int visitID)
        {
            string strSQL = CustomerReturnVisitDAL_Const.SQL_DELETE_CUSTOMERRETURNVISIT;
            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmd, DBEntityCustomerReturnVisit.FieldName.CRETURNVISITID, DbType.Int32, visitID);
            DB.ExecuteNonQuery(cmd);
        }

        public DataSet QueryCustomerReturnVisit(string sFilter)
        {
            string strSQL = CustomerReturnVisitDAL_Const.SQL_QUERY_CUSTOMERRETURNVISIT;
            if (sFilter.Trim() != string.Empty)
                strSQL += " WHERE " + sFilter;

            DbCommand cmd = DB.GetSqlStringCommand(strSQL);
            return DB.ExecuteDataSet(cmd);
        }
    }
}
